@extends('admin.admin')

@section('content')
<link rel="stylesheet" href="{{ asset('/js/bootstrap-switch/css/bootstrap-switch.min.css') }}">
<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        优惠券管理
        <small>优惠券列表</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="/admin"><i class="fa fa-dashboard"></i> 控制面板</a></li>
        <li ><a href="/admin/coupon">优惠券管理</a></li>
        <li class="active">优惠券列表</li>
    </ol>
</section>

<?php

Form::macro(
        'selectRequired',
        function($name, $options = [], $selected = null, $attributes = [], $disabled = [])
        {
            $html = '<select name="' . $name . '"';
            foreach ($attributes as $attribute => $value)
            {
                $html .= ' ' . $attribute . '="' . $value . '"';
            }
            $html .= '>';

            foreach ($options as $value => $text)
            {
                $html .= '<option value="' . $value . '"' .
                        ($value == $selected ? ' selected="selected"' : '') .
                        (in_array($value, $disabled) ? ' disabled="disabled"' : '') . '>' .
                        $text . '</option>';
            }

            $html .= '</select>';

            return $html;
        }
);?>

<!-- Main content -->
<section class="content">

    <div class="row">
        <div class="col-xs-12">

            <div class="box box-primary ">
                <div class="box-body table-responsive">

                    {!! Form::model(@$coupons,array('route' => 'admin.coupon.index','method' => 'get','class'=>'form-search form-inline pull-right')) !!}

                        <?php $select = $category::getTreeList();?>

                        <?php

                        function myspace($n,$hasChild){
                            $enum = ['├','└'];
                            if ($hasChild){
                                $space = $enum[0];
                            }else{
                                $space = $enum[1];
                            }
                            $str = '';
                            for ($i=0;$i<=$n;$i++){
                                $str.='&nbsp;&nbsp;';
                            }
                            return $str.$space;
                        }
                        $seletcItem[0] = '所有商品分类';

                        $disabledOption = [];
                        foreach ($select as $k=> $item){

                            if($item->has_children){
                                $disabledOption[]=$item->cat_id;
                            }
                            $seletcItem [$item->cat_id]= myspace($item->deepth,$item->has_children).$item->cat_name;
                        }

                        ?>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::selectRequired('cat_id',$seletcItem,@$_GET['cat_id'],['class'=>'form-control select2','id'=>'cat_id','data-placeholder'=>'所有商品分类' ],$disabledOption)!!}
                            </div>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::select('brand_id',$brandArr,@$_GET['brand_id'],['class'=>'form-control select2','data-placeholder'=>'所有品牌'])!!}
                            </div>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::select('type',$typeArr,$search['type'],['class'=>'form-control select2','data-placeholder'=>'所有类型'])!!}
                            </div>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::text('name',$search['name'],['class'=>'form-control','placeholder'=>'优惠券名称'])!!}
                            </div>
                            <button type="submit" class="btn btn-primary ">搜索</button>
                            <button type="button" onclick="window.location.href='/admin/coupon'" class="btn btn-default">重置</button>

                    {!! Form::close() !!}

                    <a href="{{route('admin.coupon.create')}}" class="btn btn-success"><i class="icon  glyphicon glyphicon-plus"></i> 添加</a>

                </div>

            </div><!-- end .box box-primary -->


            <div class="box box-primary">
                <div class="box-header">
                    <h3 class="box-title">优惠券列表</h3>
                    <div class="box-tools">
                        <a href="{{route('admin.coupon.create')}}" type="button" class="btn btn-block btn-success btn-xs"><i class="icon  glyphicon glyphicon-plus"></i> 添加</a>
                    </div>
                </div>
                <!-- /.box-header -->

                <div class="box-body table-responsive no-padding">


                    <table class="table table-hover">
                        <tr>
                            <th>ID</th>
                            <th>名称</th>
                            <th>金额</th>
                            <th>发行数量</th>
                            <th>可用类型</th>
                            <th>创建时间</th>
                            <th>结束时间</th>
                            <th>使用规则1</th>
                            <th>使用规则2</th>
                            <th>备注说明</th>
                            {{--<th style="width: 100px">编号</th>--}}

                        </tr>

                        @foreach ($coupons as $item)
                        <tr>
                            <td>{{$item->id}}</td>
                            <td>{{$item->name}}</td>
                            <td>{{$item->value}}</td>
                            <td>{{$item->amount}}</td>
                            <td>
                                <?php
                                    switch ($item->type) {
                                        case '1':
                                            echo '全场通用';
                                            break;
                                        case '2':
                                            echo '商品';
                                            break;
                                        case '3':
                                            echo '分类';
                                            break;
                                        case '4':
                                            echo '店铺';
                                            break;
                                        case '5':
                                            echo '品牌';
                                            break;

                                        default:
                                            echo '类型有误';
                                            break;
                                    }
                                ?>
                            </td>
                            <td>{{$item->created_at}}</td>
                            <td>{{$item->end_time}}</td>
                            <td>满[{{$item->limit_fir}}]元可用</td>
                            <td>每个用户可使用[1]张</td>
                            <td>{{$item->remarks}}</td>

                            <td>
                                <a href="{{Route('admin.coupon.edit',$item->id)}}?back_url=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>" type="button" class="btn  btn-default btn-xs"><i class="icon  glyphicon glyphicon-edit"></i> 修改</a>
                                <button type="button" class="btn  btn-danger btn-xs"  data-toggle="modal" data-target="#modal{{$item->id}}"><i class="icon  glyphicon glyphicon-remove"></i> 删除</button>

                                <div id="modal{{$item->id}}" class="modal fade">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title">删除警告:</h4>
                                            </div>
                                            <div class="modal-body">
                                                <p>确认要删除[{{$item->name}}]吗?</p>
                                            </div>
                                            <div class="modal-footer">


                                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                                <a data-id="{{$item->id}}" data-back="<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>" class="J-destoryCoupon btn btn-danger">删除</a>


                                            </div>
                                        </div><!-- /.modal-content -->
                                    </div><!-- /.modal-dialog -->
                                </div><!-- /.modal -->

                                <!-- <button type="button" class="btn  btn-success btn-xs"  data-toggle="modal" data-target="#modal-create-{{$item->id}}"><i
                                class="icon  glyphicon glyphicon-tasks"></i> 生成口令</button>
                                <div id="modal-create-{{$item->id}}" class="modal fade">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title">生成口令:</h4>
                                            </div>
                                            <div class="modal-body">
                                                <p>
                                                    口令个数：
                                                    <input type="number" name="number" min="1" id="pwds_number_{{$item->id}}"/>
                                                    &nbsp;&nbsp;
                                                    <label id="errMsg" style="color:red;"></label>
                                                </p>
                                            </div>
                                            <div class="modal-footer">

                                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                                <a data-id="{{$item->id}}" data-back="<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>" class="J-createPasswordCoupon btn btn-danger">确定</a>

                                            </div>
                                        </div>/.modal-content
                                    </div>/.modal-dialog
                                </div>/.modal

                                <button type="button" class="btn btn-success btn-xs"  data-toggle="modal" onclick="check_coupon_pwds({{$item->id}})"><i class="icon  glyphicon glyphicon-tasks" onclick="check_coupon_pwds()"></i> 查看口令</button>
                                <div id="modal-check-{{$item->id}}" class="modal fade">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title" id="check_modal_title_{{$item->id}}">该优惠券的口令:</h4>
                                            </div>

                                            <div class="modal-body" id="check_coupon_pwds_{{$item->id}}">

                                            </div>
                                            <div class="modal-footer">

                                                <button type="button" class="btn btn-default" data-dismiss="modal">确定</button>

                                            </div>
                                        </div>/.modal-content
                                    </div>/.modal-dialog
                                </div>/.modal

                                <button type="button" class="btn  btn-success btn-xs"  data-toggle="modal" data-target="#modal-send-{{$item->id}}"><i
                                class="icon  glyphicon glyphicon-tasks"></i> 发送短信</button>
                                <div id="modal-send-{{$item->id}}" class="modal fade">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title">发送口令短信:</h4>
                                            </div>
                                            <div class="modal-body">
                                                <p>
                                                    选择用户：</br>
                                                    <input type="radio" name="user_condation" value="0" coupon_id="{{$item->id}}" style="margin-left:80px;">&nbsp;&nbsp;全部</input></br>
                                                    <input type="radio" name="user_condation" value="1" coupon_id="{{$item->id}}" style="margin-left:80px;">&nbsp;&nbsp;近三个月登录</input></br>
                                                    <input type="radio" name="user_condation" value="2" coupon_id="{{$item->id}}" style="margin-left:80px;">&nbsp;&nbsp;邮箱注册</input></br>
                                                    </br>
                                                    人&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数：&nbsp;&nbsp;<label id="user_number_{{$item->id}}" style="color:red;">0</label>&nbsp;&nbsp;人
                                                </p>
                                            </div>
                                            <div class="modal-footer">

                                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                                <a data-id="send_msg_pwds_{{$item->id}}" data-back="<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>" coupon_id="{{$item->id}}" class="J-sendPasswordCoupon btn btn-danger">发送</a>

                                            </div>
                                        </div>/.modal-content
                                    </div>/.modal-dialog
                                </div>/.modal -->

                            </td>
                        </tr>
                        @endforeach

                    </table>

                </div>
                <!-- /.box-body -->

                <div class="box-footer">
                    <!-- 分页 -->
                    <div class="pull-right no-margin-pager">
                        {!! $coupons->appends(['type' => $search['type'], 'name' => $search['name']])->render() !!}
                    </div>

                </div>

            </div>
            <!-- /.box -->

        </div>
    </div>


<!-- Modal -->
<div class="modal fade" id="warmModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close close_btn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">信息提示</h4>
      </div>
      <div class="modal-body" id="errorMessage">

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
      </div>
    </div>
  </div>
</div>
</div>

</section>
<!-- /.content -->
<!-- Select2 -->

<script>

$('.J-destoryCoupon').on('mouseup',function(e){

    var that=$(this),coupon_id = that.data('id');


    var url = "/admin/coupon/"+coupon_id;
    var backUrl = that.data('back');
    $.ajax({
        type: "DELETE",
        url: url,
        data: {back_url:backUrl},
        dataType: "json",
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        success: function (msg) {
            //console.log(msg);
            $('#warmModal').modal('show');
            $('#modal'+coupon_id).modal('hide');
            if(msg.status){

                $('#errorMessage').html('删除成功');
                location.reload();

            }else{

                $('#errorMessage').html(msg.message);
            }//end if()

        }
    }); //end ajax

});

$('.J-createPasswordCoupon').on('mouseup',function(e){

    var that=$(this), coupon_id = that.data('id'), pwds_number = $('#pwds_number_'+coupon_id).val();

    if (pwds_number<1) {

        $("#errMsg").html('数量不能为空且必须大于0');
    }else{

        var url = "/admin/password_coupon/create_pwd/"+pwds_number+"/"+coupon_id;

        $.ajax({
            type: "POST",
            url: url,
            data: "",
            dataType: "json",
            headers: {
                'X-CSRF-Token': "{{csrf_token()}}"
            },
            success: function (msg) {
                // console.log(msg);
                $('#modal-create-'+coupon_id).modal('hide');
                $('#warmModal').modal('show');
                if(msg.status){

                    $('#errorMessage').html('生成成功');

                    location.reload();
                }else{

                    $('#errorMessage').html(msg.message);
                }//end if()
            }
        }); //end ajax
    }

});

function check_coupon_pwds(coupon_id){

    var url = "/admin/password_coupon/get_pwds_by_coupon_id";

    var parmData = {};

    parmData.coupon_id = coupon_id;

    $.ajax({
        type: "POST",
        url: url,
        data: parmData,
        dataType: "json",
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        success: function (msg) {

            // console.log(msg);

            $('#modal-check-'+coupon_id).modal('show');

            if(msg.status){

                $('#check_modal_title_'+coupon_id).html('该优惠券的口令');

                cont = "<ul>";

                $.each(msg.data, function(i, item){

                    cont += "<li>";

                    switch(i){

                        case(0):

                            cont += "未发送";
                        break;

                        case(1):

                            cont += "已发送";
                        break;

                        case(2):

                            cont += "已激活";
                        break;
                    }

                    cont += "：" + item + "个</li>";
                });

                cont += "</ul>";

                $('#check_coupon_pwds_'+coupon_id).html(cont);
            }else{

                $('#check_modal_title_'+coupon_id).html('查询失败');

                $('#check_coupon_pwds_'+coupon_id).html(msg.message);
            }//end if()
        }
    }); //end ajax
}

$(":radio").on('click', function(argument) {

    var parmData = {};

    user_condition = $(this).val();

    coupon_id = $(this).attr("coupon_id");

    parmData.user_condition = user_condition;

    url = "/admin/user/get_user_num_by_condition";

    $.ajax({
        type: "GET",
        url: url,
        data: parmData,
        dataType: "json",
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        success: function (msg) {
            // console.log(msg);

            $('#user_number_'+coupon_id).html(msg.data);
        }
    }); //end ajax
});

//发送优惠券口令给用户
// $('.J-sendPasswordCoupon').on('mouseup',function(e){
$('.J-sendPasswordCoupon').on('click',function(e){

    var parmData = {};

    coupon_id = $(this).attr("coupon_id");

    user_status = $('#modal-send-' + coupon_id + ' input[name="user_condation"]:checked').val();

    parmData.coupon_id = coupon_id;

    parmData.user_status = user_status;

    url = "/admin/password_coupon/send_pwds_to_user";

    $.ajax({
        type: "POST",
        url: url,
        data: parmData,
        dataType: "json",
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        success: function (msg) {
            // console.log(msg);

            $('#modal-send-'+coupon_id).modal('hide');

            $('#warmModal').modal('show');

            $('#errorMessage').html(msg.message);
        }
    }); //end ajax

});

</script>
@endsection